﻿using UnityEngine;
using System;

[Serializable]
public class BusInformationModel 
{
	/// <summary>
	/// Tempo da ultima atualizacao da das informacoes
	/// </summary>
	public DateTime dateTime { get; private set; }

	/// <summary>
	/// Identificador do onibus (seu numero/ordem)
	/// </summary>
	public string identifier { get; private set; }

	/// <summary>
	/// Linha do onibus (pode ser alfanumerica)
	/// </summary>
	public string line { get; private set; }

	/// <summary>
	/// Latitude
	/// </summary>
	public float latitude { get; private set; }

	/// <summary>
	/// Longitude
	/// </summary>
	public float longitude { get; private set; }

	/// <summary>
	/// Velocidade do onibus no momento do registro
	/// </summary>
	public float velocity { get; private set; }

	/// <summary>
	/// Se os dados sao validos para serem utilizados no sistema
	/// </summary>
	public Boolean isValid { get; private set; }

	public BusInformationModel(string[] datas)
	{
		Setup(
			datas[0].ToString(),
			datas[1].ToString(),
			datas[2].ToString(),
			datas[3].ToString(),
			datas[4].ToString(),
			datas[5].ToString()
		);
	}

	public BusInformationModel(SimpleJSON.JSONArray datas)
	{
		if(datas == null) return;

		Setup(
			datas[0].ToString(),
			datas[1].ToString(),
			datas[2].ToString(),
			datas[3].ToString(),
			datas[4].ToString(),
			datas[5].ToString()
		);
	}

	private void Setup(string dateTime, string identifier, string line, string latitude, string longitude, string velocity)
	{
		//ajustes das strings 
		//removo as aspas
		dateTime = dateTime.Replace("\"", "");
		identifier = identifier.Replace("\"", "");
		line = line.Replace("\"", "");
		latitude = latitude.Replace("\"", "");
		longitude = longitude.Replace("\"", "");
		velocity = velocity.Replace("\"", "");

		//se a linha for apenas numerica eu a transformo em um "int"
		line = line.Replace(".0", "");

		//o dado so eh valido se existir as coordenadas e a linha do onibus
		if(string.IsNullOrEmpty(latitude) || string.IsNullOrEmpty(longitude) || string.IsNullOrEmpty(line))
		{
			this.isValid = false;
			return;
		}


		this.isValid = true;
		this.identifier = string.IsNullOrEmpty(identifier) ? "null" : identifier;
		this.line = string.IsNullOrEmpty(line) ? "null" : line;
		this.velocity = float.Parse(velocity);
		this.latitude = float.Parse(latitude);
		this.longitude = float.Parse(longitude);

		DateTime d;
		DateTime.TryParse(dateTime, out d);
		this.dateTime = d;
	}

	/*
	private DateTime TryParseDateTime(string value)
	{
		DateTime v;
		if(DateTime.TryParse(value, out v))
		{
			return v;
		}
		return new DateTime();
	}
	*/
}
